//
//  CLUser.m
//  chanlin
//
//  Created by Ryan on 14-11-24.
//  Copyright (c) 2014年 chanlin. All rights reserved.
//

#import "CLUser.h"

@implementation CLUser

- (void)save{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    [dic sgrSetObject:self.uid forKey:@"uid"];
    [dic sgrSetObject:self.token forKey:@"token"];
    [dic sgrSetObject:self.nickName forKey:@"nickName"];
    [dic sgrSetObject:self.imgUrl forKey:@"imgUrl"];
    [dic sgrSetObject:self.groupId forKey:@"groupId"];
    [dic sgrSetObject:self.openId forKey:@"openId"];
    [dic sgrSetObject:self.site forKey:@"site"];
    [dic sgrSetObject:@(self.status) forKey:@"status"];
    [dic sgrSetObject:self.email forKey:@"email"];
    [dic sgrSetObject:self.password forKey:@"password"];
    NSString *path=[NSString stringWithFormat:@"%@/%@",[self k_cacheUrl],@"user"];
    NSFileManager *manager=[NSFileManager defaultManager];
    if([manager fileExistsAtPath:path]){
        [manager removeItemAtPath:path error:nil];
    }
    
    [dic writeToFile:path atomically:YES];
}

+ (CLUser *)deSave{
    NSString *path=[NSString stringWithFormat:@"%@/%@",[self k_cacheUrl],@"user"];
    NSDictionary *dic=[NSDictionary dictionaryWithContentsOfFile:path];
    if(dic){
        CLUser *user=[self new];
        user.groupId=[dic sgrFGetStringForKey:@"groupId"];
        user.site=[dic sgrFGetStringForKey:@"site"];
        user.openId=[dic sgrFGetStringForKey:@"openId"];
        user.uid=[dic sgrGetStringForKey:@"uid"];
        user.token=[dic sgrGetStringForKey:@"token"];
        user.nickName=[dic sgrGetStringForKey:@"nickName"];
        user.imgUrl=[dic sgrGetStringForKey:@"imgUrl"];
        user.status=[[dic sgrGetNumberForKey:@"status"] intValue];
        user.email=[dic sgrGetStringForKey:@"nickName"];
        user.password=[dic sgrGetStringForKey:@"password"];
        return user;
    }
    return nil;
}

+ (void)clear{
    NSString *path=[NSString stringWithFormat:@"%@/%@",[self k_cacheUrl],@"user"];
    NSFileManager *manager=[NSFileManager defaultManager];
    if([manager fileExistsAtPath:path]){
        [manager removeItemAtPath:path error:nil];
    }
}

@end
